package xyz.jangle.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.jangle.dao.DemoMapper;
import xyz.jangle.model.Demo;
import xyz.jangle.service.DemoService;
import xyz.jangle.utils.JConstant;

@Service
public class DemoServiceImpl implements DemoService {
	
	@Autowired
	private DemoMapper demoMapper;

	@Override
	public Map<String, Object> selectPage(Demo record) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Demo> list = demoMapper.selectPage(record);
		Long count = demoMapper.selectPageCount(record);
		map.put("list", list);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("count", count);
		return map;
	}

	@Override
	public Map<String, Object> selectByPrimaryKey(Demo record) {
		Map<String, Object> map = new HashMap<String, Object>();
		Demo demo = demoMapper.selectByPrimaryKey(record.getId());
		map.put("model", demo);
		map.put("code", JConstant.SUCCESS_CODE);
		return map;
	}

	@Override
	public Map<String, Object> insertOrUpdate(Demo record) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(record.getId() == null) {
			demoMapper.insert(record);
		}else {
			demoMapper.updateByPrimaryKey(record);
		}
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> batchDeleteByPrimaryKey(Demo record) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(record.getIds()!=null) {
			record.setIdsArray(record.getIds().split(","));
		}else {
			record.setIdsArray(new String[0]);
		}
		demoMapper.batchDeleteByPrimaryKey(record);
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> deleteByPrimaryKey(Demo record) {
		Map<String, Object> map = new HashMap<String, Object>();
		demoMapper.deleteByPrimaryKey(record.getId());
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}
	
	
	

}
